package com.samshow.base.program;

import com.samshow.baseVo.CaptionRequest;
import com.samshow.res.entity.ScreenEntity;
import com.samshow.res.vo.DefaultBxTextProgram;
import onbon.bx05.Bx5GScreenProfile;
import onbon.bx05.area.BxArea;
import onbon.bx05.area.TextCaptionBxArea;
import onbon.bx05.area.page.BxPage;
import onbon.bx05.area.page.TextBxPage;
import onbon.bx05.file.BxFile;
import onbon.bx05.file.ProgramBxFile;
import onbon.bx05.series.Bx5G;
import onbon.bx05.utils.DisplayStyleFactory;
import onbon.bx05.utils.TextBinary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by niwb on 2017/2/21.
 */
public class TextProgramFactory extends ProgramFactory {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    //private String defaultNme = "p001";

    private CaptionRequest captionRequest;

    private Bx5GScreenProfile profile;

    private DefaultBxTextProgram defaultBxTextProgram;

    public TextProgramFactory(ScreenEntity screen, CaptionRequest captionRequest,DefaultBxTextProgram defaultBxTextProgram) {
        super.screen = screen;
        this.captionRequest = captionRequest;
    }

    @Override
    public BxFile createBxFile() {
        ProgramBxFile programBxFile = null;
        try {
            if (profile == null) {
                profile = new Bx5GScreenProfile(screen.getWidth(), screen.getHeight(), new Bx5G(), Bx5GScreenProfile.ScreenColorType.SINGLE);
            }
            programBxFile = new ProgramBxFile(captionRequest.getProgramName(), profile);
            // 是否显示节目边框
            programBxFile.setFrameShow(defaultBxTextProgram.isFrameShow());
            // 节目边框的移动速度  框显示速度，1 - 48
            programBxFile.setFrameSpeed(defaultBxTextProgram.getFrameSpeed());
            // 使用第几个内置边框 内建效果编号，双基色 1 ~ 18，单基色 1 ~ 14，全彩 1 ~ 17。
            programBxFile.loadFrameImage(defaultBxTextProgram.getLoadFrameImage());
        } catch (IOException e) {
            log.error("createBxFile ERROR", e);
        }

        return programBxFile;
    }

    @Override
    public BxArea createBxArea() {
        TextCaptionBxArea tArea = null;
        try {
            if (profile == null) {
                profile = new Bx5GScreenProfile(screen.getWidth(), screen.getHeight(), new Bx5G(), Bx5GScreenProfile.ScreenColorType.SINGLE);
            }
            tArea = new TextCaptionBxArea(defaultBxTextProgram.getX(), defaultBxTextProgram.getY(), screen.getWidth(), screen.getHeight(), profile);
            // 使能区域边框
            tArea.setFrameShow(defaultBxTextProgram.isAreaFrameShow());
            // 使用内置边框3
            tArea.loadFrameImage(defaultBxTextProgram.getAreaFrameImage());
        } catch (IOException e) {
            log.error("createBxArea ERROR", e);
        }
        return tArea;
    }

    @Override
    public List<BxPage> createBxPage() {
        List<BxPage> list = new ArrayList<>();
        String captions = captionRequest.getCaptions();
        String[] captionArray = captions.split(";");
        TextBxPage page;
        for (String caption : captionArray) {
            page = new TextBxPage(caption);
            // 对文本的处理是否自动换行
            page.setLineBreak(true);
            // 设置文本水平对齐方式
            page.setHorizontalAlignment(TextBinary.Alignment.valueOf(defaultBxTextProgram.getHorizontalAlignment()));
            // 设置文本垂直居中方式
            page.setVerticalAlignment(TextBinary.Alignment.valueOf(defaultBxTextProgram.getVerticalAlignment()));
            // 设置文本字体
            page.setFont(new Font(defaultBxTextProgram.getFontName(), Font.PLAIN, defaultBxTextProgram.getFontSize()));         // 字体
            // 设置文本颜色
            page.setForeground(new Color(defaultBxTextProgram.getForeground()));
            // 设置区域背景色，默认为黑色
            page.setBackground(new Color(defaultBxTextProgram.getBackground()));
            // 调整特技方式
            page.setDisplayStyle(DisplayStyleFactory.getStyle(defaultBxTextProgram.getDisplayStyle()));
            // 调整特技速度
            page.setSpeed(defaultBxTextProgram.getSpeed());
            // 调整停留时间, 单位 10ms
            page.setStayTime(defaultBxTextProgram.getStayTime());
            list.add(page);
        }
        return list;
    }


}
